package com.wan.data.mysql.orm.convert.auto;

import com.wan.data.mysql.orm.convert.AutoConverter;
import com.wan.data.mysql.orm.meta.ColumnMeta;
import com.wan.data.mysql.util.DbColumnType;

public class CharConverter implements AutoConverter {
    @Override
    public Class<?>[] convertType() {
        return new Class[]{char.class,Character.class};
    }

    @Override
    public String dbType(long len, int pointLen) {
        return getDbType().format(1,1);
    }

    @Override
    public DbColumnType getDbType() {
        return DbColumnType.CHAR;
    }

    @Override
    public Object dbToEntity(ColumnMeta columnMeta, Object value) throws Exception {
        String string = value.toString();
        if(string.length() > 0){
            return string.charAt(0);
        }
        return Character.MIN_VALUE;
    }

    @Override
    public Object entityToDb(ColumnMeta columnMeta, Object value) throws Exception {
        char charValue = (char) value;
        return String.valueOf(charValue);
    }
}
